<script setup>
import { defineProps,ref } from 'vue';
import { DownloadOutlined, EyeOutlined } from '@ant-design/icons-vue';
import PdfPreview from "@/components/PdfPreview.vue";
import {FilePdfOutlined} from '@ant-design/icons-vue'
import {message} from 'ant-design-vue'

// 接收简历文件信息作为 prop
const props = defineProps({
  file: {
    type: JSON,
    required: true
  },
});
const fileType = 'pdf'
const pdfPreviewRef = ref(null)
const buttonSize =ref('5')

// 文件点击预览
const onPreview = async () => {
  try {
    pdfPreviewRef.value.togglePreview()
  } catch (error) {
    message.error('文件预览失败: ' + error.message);
  }
};

const onDownload = () => {
  // 获取文件的完整 URL
  const downloadUrl = props.file?.filePath;

  // 创建一个临时链接
  const link = document.createElement('a');
  link.href = downloadUrl;
  link.download = '个人简历'; // 设置下载的文件名
  document.body.appendChild(link);

  // 触发点击事件来下载文件
  link.click();

  // 下载完成后移除链接
  document.body.removeChild(link);
};
</script>

<template>
  <a-space direction="vertical">
    <a-space class="message-bubble" >
      <div class="pdf-icon">
        <FilePdfOutlined style="font-size: 30px"/>
      </div>
      <a-space direction="vertical" class="pdf-button-container" :size="buttonSize">
        <a-button @click="onPreview(file?.filePath)" type="link" size="small">
          <EyeOutlined /> 预览
        </a-button>
        <a-button @click="onDownload(file?.filePath)" type="link" size="small">
          <DownloadOutlined /> 下载
        </a-button>
      </a-space>
      <PdfPreview ref="pdfPreviewRef" :preview-url="file?.filePath" :file-type="fileType"/>
    </a-space>
    <div class="pdf-name">
      {{file?.fileName}}
    </div>
  </a-space>
</template>

<style scoped>
.message-bubble {
  display: flex;
  gap: 10px;
  background-color: #e6f7ff;
  border-radius: 12px;
  width: 100px;
}

.pdf-icon {
  width: 100%;
  height: 40px;
  display: flex;
  align-items: center;
}

.pdf-button-container {
  width: 100%;
  display: flex;
  justify-content: space-between;
}

.pdf-name {
  border-top: 1px solid black;
  padding-top: 5px;
}
</style>
